using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Principal;
using System.Web.Mvc;
using System;
using System.Web.SessionState;
using SPConf.Models;

namespace SPConf.Extensions
{
  public static class EnumExtensions
  {

    public static string GetName(this IPrincipal user)
    {
      try
      {
        ConfUsersRepository repository = new ConfUsersRepository();
        string name = repository.GetBy(user.Identity.Name).FullName.Trim();
        if (string.IsNullOrEmpty(name))
          name = user.Identity.Name;
        return name;
      }
      catch
      {
        return user.Identity.Name;
      }
    }

    public static IEnumerable<SelectListItem> GetItems<TEnum>(this TEnum @enum)
    {
      var values = Enum.GetValues(typeof(TEnum))
        .Cast<TEnum>()
        .Select(e => new { Id = e, Name = e.ToString().Locale() });

      return new SelectList(values, "Id", "Name", @enum);
    }

    public static string Locale(this string value)
    {
      string result = Resources.ResourceManager.GetString(value);
      if (string.IsNullOrEmpty(result))
      {
        return value;
      }
      return result;
    }
  }
}